<?php
//多文件上传面向过程封装
function getFiles()
{
    $i =0;
    foreach($_FILES as $file)
    {
        if(is_string($file['name']))
        {
            $files[$i]=$file;
            $i++;
        }elseif(is_array($file['name']))
        {
            foreach($file['name'] as $key=>$val)
            {
                $files[$i]['name'] = $file['name'][$key];
                $files[$i]['type'] = $file['type'][$key];
                $files[$i]['tmp_name'] = $file['tmp_name'][$key];                
                $files[$i]['error'] = $file['error'][$key];
                $files[$i]['size'] = $file['size'][$key]; 
                $i++;               
            }
        }
    }
    return $files;
}

function uploadFile($fileInfo, $flag=true,$maxSize = 2097152,$path = 'up',
$allowExt = array('jepg','jpg','gif','wbmp','png')){
    //判断错误号
    // $flag=true;
    // $allowExt = array('jepg','jpg','gif','wbmp');
    if($fileInfo['error'] ===UPLOAD_ERR_OK)
    {
            //检测上传文件大小
            if($fileInfo['size']>$maxSize)
            {
                $res['mes'] = $fileInfo['name']."文件上传过大";
            }
            //检测上传文件的类型
            $ext=getExt($fileInfo['name']);
            if(!in_array($ext,$allowExt))
            {
                $res['mes']=$fileInfo['name'].'非法文件类型';
            }

            //检测是否为真实图片类型
            if($flag){
                if(!getimagesize($fileInfo['tmp_name']))
                {
                   $res['mes']=$fileInfo['name'].'不是真实文件类型';
                }
             }
             //判断文件上传是否是http post
            if(!is_uploaded_file($fileInfo['tmp_name']))
            {
                $res['mes']=$fileInfo['name'].'文件不是通过http post方式上传的';
            }
            if($res){
                return $res;
            } 
           // $path = 'up';
            $umiName = getUniName();
            $destination=$path.'/'.$umiName.'.'.$ext;
            if(!file_exists($path))
            {
                mkdir($path,0777,true);
                chmod($path,0777);
            }
            if(!move_uploaded_file($fileInfo['tmp_name'],$destination))
            {
                $res['mes']= $fileInfo['name'].'文件上传失败';
            }
            $res['mes']=$fileInfo['name'].'文件上传成功';
            $res['dest']=$destination;
            return $res;
        
    }else{
        //匹配错误信息
        switch($fileInfo['error']){
            case 1:
                $res['mes'] = "上传文件超过了PHP配置upload_max_filessize的值";
                break;
            case 2:
                $res['mes'] = "超过了表单max_file_size限制的大小";
                break;
            case 3:
                $res['mes']  ="文件部分被上传";
                break;
            case 4:
                $res['mes']  = "没有选择上传文件";
                break;
            case 6:
                $res['mes']  = "没有找到临时目录";
                break;
            case 7:
            case 8:
                $res['mes']  = "系统错误";
                break;
        }
        return $res;
    }
}